<template>
  <div class="JoinContainer">
    <div class="Join-Box">
      <h1>加入团队</h1>
      <form @submit.prevent="joinTeam">
        <div class="input-group">
          <label for="teamname">团队名称</label>
          <input type="teamname" id="teamname" v-model="inputteamname" placeholder="请输入团队名称" required />
        </div>
        <div class="input-group">
          <label for="teamaccess">团队邀请码</label>
          <input type="teamaccess" id="teamaccess" v-model="inputteamaccess" placeholder="请输入团队邀请码" required />
        </div>
        <div class="button-group">
          <button class="back-btn" @click="BackHome">返回</button>
          <button type="submit" class="submit-btn">加入团队</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useTeamStore } from '../stores/teamStore.ts';
import { useServer } from '../components/ts/useServer.ts';
import { useRouter } from 'vue-router';

const router = useRouter();
const teamStore = useTeamStore();
const { post } = useServer();
const testUrl = 'https://m1.apifoxmock.com/m1/6951272-6668040-default/api'

// 表单输入相关
const inputteamname = ref('');
const inputteamaccess = ref('');

// 返回主页函数
const BackHome = () => {
    router.push('/team');
}

// 加入团队主函数
const joinTeam = async () => {
  if (!(inputteamname.value && inputteamaccess.value)) {alert('Please fill in all fields');return;}
  try {
    post('team/join', { inputteamname: inputteamname.value, inputteamaccess: inputteamaccess.value }, testUrl).then(res => {
      if (res.success) {
        const newTeam = {
          id: res.teamId,
          thumbnail: res.teamAvatar,
          name: inputteamname.value,
          description: res.description,
          admin: res.teamleader,
          openness: res.openness,
          member_count: res.member_count + 1,
          projects: res.projects,
          access: "",
        };
        teamStore.teams.push(newTeam);
      }
    })
  } catch(error) {
    console.error('加入团队失败', error);
    teamStore.error = '加入团队失败，请稍后再试';
  }
  if (!teamStore.error) {
    BackHome();
  } else {
    alert(teamStore.error);
  }
}

</script>

<style scoped>
/* 大背景 */
.JoinContainer {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
}

/* 加入团队框 */
.Join-Box {
  background-color: #fff;
  padding: 30px;
  border-radius: 12px; 
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05); 
  width: 100%;
  max-width: 400px;
}

.Join-Box h1 {
  font-size: 28px;
  color: #333; 
  margin-bottom: 20px;
  text-align: left;
  font-weight: 600;
  border-bottom: 2px solid #e4e1e1;
}

.input-group {
  margin-bottom: 20px;
}

/* 标签样式 */
.input-group label {
  display: block;
  font-size: 14px;
  color: #666;
  margin-bottom: 6px;
  font-weight: 500;
}

/* 输入框通用样式 */
.input-group input {
  width: 100%;
  padding: 10px;
  border-radius: 6px;
  border: 1px solid #e0e0e0;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s ease;
}

/* 输入框聚焦样式 */
.input-group input:focus {
  border-color: #4096ff;
}

/* 按钮组容器 */
.button-group {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}

/* 返回按钮样式 */
.back-btn {
  padding: 10px 20px;
  background-color: #f0f0f0;
  color: #666;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.2s ease;
}

/* 创建按钮 */
.submit-btn {
  padding: 12px;
  background-color: #4096ff; /* 蓝色按钮 */
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.2s ease;
  width: calc(60% - 100px); 
}

.back-btn:hover,
.submit-btn:hover {
  transform: translateY(-2px);
}
.back-btn:hover {
  background-color: #e0e0e0;
}
.submit-btn:hover {
  background-color: #2e85e6;
}
</style>